layui.use(['form','treeSelect','layer','upload','laydate'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;
    var laydate = layui.laydate;

    //回显数据
    var request = GetRequest();
    $.get("/netword/equipment/" + request.id,function (data) {
        if (data.data.length != 0) {
            var netword = data.data[0];
            console.log(netword);
            $("#id").val(netword.id);
            $("#location").val(netword.location);
            $("#simCard").val(netword.simCard);
            $("#smsNo").val(netword.smsNo);
            $("#remarks").val(netword.remarks);

            //所属网络类型
            treeSelect.render({
                elem: '#tree-select-network',
                data: '/network/type/tree/select',
                type: 'get',
                placeholder: '请选择网络类型',// 占位符
                search: true,// 是否开启搜索功能：true/false，默认false
                click: function(data){
                    $("#tree-select-network").val(data.current.id);
                },
                success: function (item) { // 加载完成后的回调函数 areaId
                    if (netword.networkTypeId != null && netword.networkTypeId != "") {
                        $("#tree-select-network").val(netword.networkTypeId);
                    } else {
                        $("#tree-select-network").val("0");
                    }
                    treeSelect.checkNode('tree-select-network',netword.networkTypeId);
                }
            });

            //加载网络深度信息
            $.get("/dictionary/list/764b192197514d9f8fea7753ee911daa",function (deep) {
                $.each(deep.data,function (i,ele) {
                    if (ele.value == netword.deep) {
                        $("#deep").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                    } else {
                        $("#deep").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                    }
                });
                form.render('select');
            });
            //加载连接方式信息
            $.get("/dictionary/list/d396f2b9d1df48038eaceff90e3f6546",function (linkType) {
                $.each(linkType.data,function (i,ele) {
                    if (ele.value == netword.linkType) {
                        $("#linkType").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                    } else {
                        $("#linkType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                    }
                });
                form.render('select');
            });
            //加载应用类型信息
            $.get("/dictionary/list/3d38edfdd55a47aeadc3ab1998e44292",function (applicationType) {
                $.each(applicationType.data,function (i,ele) {
                    if (ele.value == netword.applicationType) {
                        $("#applicationType").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                    } else {
                        $("#applicationType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                    }
                });
                form.render('select');
            });
            //加载协议类型信息
            $.get("/dictionary/list/38d8f3e1ec8540f5b0b9b568d38082b7",function (protocolType) {
                $.each(protocolType.data,function (i,ele) {
                    if (ele.value == netword.protocolType) {
                        $("#protocolType").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                    } else {
                        $("#protocolType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                    }
                });
                form.render('select');
            });

            //修改网络信息
            form.on('submit(edit)', function(data){
                var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                $.ajax({
                    url: '/netword/edit',
                    data:data.field,
                    type:'POST',
                    success: function (data) {
                        if(data.code == 200) {
                            top.layer.close(index);
                            top.layer.msg("修改成功！");
                            layer.closeAll("iframe");
                            //刷新父页面
                            parent.location.reload();
                        } else {
                            top.layer.close(index);
                            top.layer.msg("修改失败！");
                        }
                    },
                    error:function () {
                        top.layer.close(index);
                        top.layer.msg("修改失败！");
                    }
                });
                return false;
            });
        } else {
            //所属网络类型
            treeSelect.render({
                elem: '#tree-select-network',
                data: '/network/type/tree/select',
                type: 'get',
                placeholder: '请选择网络类型',// 占位符
                search: true,// 是否开启搜索功能：true/false，默认false
                click: function(data){
                    $("#tree-select-network").val(data.current.id);
                },
                success: function (item) { // 加载完成后的回调函数 areaId
                }
            });

            //加载网络深度信息
            $.get("/dictionary/list/764b192197514d9f8fea7753ee911daa",function (deep) {
                $.each(deep.data,function (i,ele) {
                    $("#deep").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                });
                form.render('select');
            });
            //加载连接方式信息
            $.get("/dictionary/list/d396f2b9d1df48038eaceff90e3f6546",function (linkType) {
                $.each(linkType.data,function (i,ele) {
                    $("#linkType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                });
                form.render('select');
            });
            //加载应用类型信息
            $.get("/dictionary/list/3d38edfdd55a47aeadc3ab1998e44292",function (applicationType) {
                $.each(applicationType.data,function (i,ele) {
                    $("#applicationType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                });
                form.render('select');
            });
            //加载协议类型信息
            $.get("/dictionary/list/38d8f3e1ec8540f5b0b9b568d38082b7",function (protocolType) {
                $.each(protocolType.data,function (i,ele) {
                    $("#protocolType").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                });
                form.render('select');
            });
            //修改网络信息
            form.on('submit(edit)', function(data){
                data.field.equipmentId = request.id;
                var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                $.ajax({
                    url: '/netword/add',
                    data:data.field,
                    type:'POST',
                    success: function (data) {
                        if(data.code == 200) {
                            top.layer.close(index);
                            top.layer.msg("修改成功！");
                            layer.closeAll("iframe");
                            //刷新父页面
                            parent.location.reload();
                        } else {
                            top.layer.close(index);
                            top.layer.msg("修改失败！");
                        }
                    },
                    error:function () {
                        top.layer.close(index);
                        top.layer.msg("修改失败！");
                    }
                });
                return false;
            });
        }
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});